// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.RSS;
using svs.Mobile.BeyondMedia.UiCore.KineticControls;

namespace svs.Mobile.BeyondMedia.Objects
{
	public static class FeedImageList
	{
		private static readonly List<ImageData> m_CachedImages = new  List<ImageData>(20);
		private static readonly Dictionary<Guid, int> m_FeedImageIndexMap = new Dictionary<Guid, int>();
		static int[] _SupportedDPIs = new int[] { 96, 131, 128, 192 };
    
		/// <summary>
		/// Image used if the feed does not have an image
		/// </summary>
		public static ImageData DefaultFeedImage
		{
			get { return m_CachedImages[0]; }
		}

		/// <summary>
		/// Image used if the feed is an OPML feed
		/// </summary>
		public static ImageData DefaultOpmlImage
		{
			get
			{
				return m_CachedImages[1];
			}
		}

		/// <summary>
		/// Image used if the feed is an Audio Folder
		/// </summary>
		public static ImageData DefaultAudioFolderImage
		{
			get
			{
				return m_CachedImages[2];
			}
		}

		/// <summary>
		/// Image used if the feed is an Audio Folder
		/// </summary>
		public static ImageData DefaultSubAudioFolderImage
		{
			get
			{
				return m_CachedImages[3];
			}
		}

		/// <summary>
		/// Image used for the update failed
		/// </summary>
		public static ImageData WarnOverlay
		{
			get
			{
				return m_CachedImages[5];
			}
		}

		public static ImageData LockedOverlay
		{
			get
			{
				return m_CachedImages[4];
			}
		}

		public static ImageData FeedHasPodcastImage
		{
			get
			{
				return m_CachedImages[6];
			}
		}

		public static ImageData TrackImage
		{
			get
			{
				return m_CachedImages[7];
			}
		}

		public static ImageData ExternalTrackImage
		{
			get
			{
				return m_CachedImages[8];
			}
		}

		public static ImageData DownloadingTrackImage
		{
			get
			{
				return m_CachedImages[9];
			}
		}

		public static ImageData MoreArrowImage
		{
			get
			{
				return m_CachedImages[10];
			}
		}

		public static ImageData GoUpImage
		{
			get
			{
				return m_CachedImages[11];
			}
		}

		public static ImageData DefaultExternalFeedImage
		{
			get
			{
				return m_CachedImages[12];
			}
		}

		public static ImageData PlayingTrackImage
		{
			get
			{
				return m_CachedImages[13];
			}
		}

		public static ImageData PausedTrackImage
		{
			get
			{
				return m_CachedImages[14];
			}
		}

		public static ImageData PlayedTrackImage
		{
			get
			{
				return m_CachedImages[15];
			}
		}

		public static ImageData PlayedExternalTrackImage
		{
			get
			{
				return m_CachedImages[16];
			}
		}

		public static ImageData GetFeedImageFor(Feed feed)
		{
			try
			{
				if (!m_FeedImageIndexMap.ContainsKey(feed.ID))
				{
					if (FeedImageCache.HasCachedImageFor(feed))
					{
						m_CachedImages.Add(((new ImageData(FeedImageCache.GetFeedImagePath(feed)))));
						m_FeedImageIndexMap[feed.ID] = m_CachedImages.Count - 1;
					}
					else
						return DefaultFeedImage;
				}

				return m_CachedImages[m_FeedImageIndexMap[feed.ID]];
			}
			catch(Exception ex)
			{
				CoreHelper.WriteLogEntry("Unable to load image for: " + FeedImageCache.GetFeedImagePath(feed)  + "! Reason:" + ex);
			}

			return DefaultFeedImage;
		}

		public static string GetFeedImagePathFor(Feed feed)
		{
			return FeedImageCache.HasCachedImageFor(feed) ? FeedImageCache.GetFeedImagePath(feed) : string.Empty;
		}

		public static void Initialize(Size  imageSize, int screenDpi)
		{
			m_FeedImageIndexMap.Clear();
			m_CachedImages.Clear();
			m_CachedImages.Add(LoadImageForDPI("DefaultFeedImage.png", screenDpi, Color.Red));  //0
			m_CachedImages.Add(LoadImageForDPI("DefaultOpmlImage.png", screenDpi, Color.Red));  //1
			m_CachedImages.Add(LoadImageForDPI("FolderAudio.png", screenDpi, Color.Red));       //2
			m_CachedImages.Add(LoadImageForDPI("SubFolderAudio.png", screenDpi, Color.Red));    //3

			m_CachedImages.Add(LoadImageForDPI("locked.png", screenDpi, Color.White));           //4
			m_CachedImages.Add(LoadImageForDPI("warn.png", screenDpi, Color.Red));		  			 //5
			m_CachedImages.Add(LoadImageForDPI("podcasts.png", screenDpi, Color.White));				 //6

			m_CachedImages.Add(LoadImageForDPI("track.png", screenDpi, Color.White));						 //7
			m_CachedImages.Add(LoadImageForDPI("externalTrack.png", screenDpi, Color.White));		 //8

			var dnl = LoadImageForDPI("Downloading.png", screenDpi, Color.White);								 //9	
			dnl.ViewportSize = new Size(21,36);
			m_CachedImages.Add(dnl);																																																			 //9

			m_CachedImages.Add(LoadImageForDPI("MoreArrow.png", screenDpi, Color.White));		     //10
			m_CachedImages.Add(LoadImageForDPI("GoUp.png", screenDpi, Color.White));						 //11
			m_CachedImages.Add(LoadImageForDPI("DefaultExternalFeedImage.png", screenDpi, Color.Red));		 //12
			m_CachedImages.Add(LoadImageForDPI("playingTrack.png", screenDpi, Color.White));		 //13
			m_CachedImages.Add(LoadImageForDPI("pausedTrack.png",screenDpi, Color.White));		   //14
			m_CachedImages.Add(LoadImageForDPI("playedTrack.png", screenDpi, Color.White));		   //15
			m_CachedImages.Add(LoadImageForDPI("playedExternalTrack.png", screenDpi, Color.White));		   //16

			FeedImageCache.Initialize(imageSize);
		}


		static ImageData LoadImageForDPI(string name, int DPI, Color transparentColor)
		{
			bool pathResolved = false;

			int currentDPI = DPI;
			string imagePath = null;
			bool tried131 = false;

			while(!pathResolved)
			{
				imagePath = GetImagePathForDPI(name, currentDPI);

				if(imagePath != null)
					break;

				switch(currentDPI)
				{
					case 192:
						currentDPI = 131;
						break;

					case 131:
						tried131 = true;
						currentDPI = 128;
						break;

					case 128:
						if (!tried131)
						{
							imagePath = GetImagePathForDPI(name, 131);
							if(imagePath != null)
								break;
						}

						currentDPI = 96;
						break;

					case 96:
						imagePath = string.Empty;
						pathResolved = true;
						break;

					default:
						currentDPI = 96;
						break;
				}
			}

			return new ImageData(imagePath, transparentColor);
		}

		private static string GetImagePathForDPI(string name, int dpi)
		{
			var path = string.Format(@"{0}\img\{1}\{1}_{2}", Configuration.RssTemplatesPath, dpi, name);
			return File.Exists(path) ? path : null;
		}

		
	}
}